package com.pdsu.graduation_design;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.Window;
import android.widget.RadioButton;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pdsu.graduation_design.adapter.M_FragmentPagerAdapter;
import com.pdsu.graduation_design.domain.Commodity;
import com.pdsu.graduation_design.domain.Order;
import com.pdsu.graduation_design.domain.User;
import com.pdsu.graduation_design.dto.OrderDTO;
import com.pdsu.graduation_design.dto.PayCommodityDTO;
import com.pdsu.graduation_design.dto.Store;
import com.pdsu.graduation_design.listener.Bottom_Tap_OnClickListener;
import com.pdsu.graduation_design.utils.JsonUtil;
import com.pdsu.graduation_design.utils.Result;
import com.pdsu.graduation_design.views.Main_Fragment;
import com.pdsu.graduation_design.views.My_Fragment;
import com.pdsu.graduation_design.views.News_Fragment;
import com.pdsu.graduation_design.views.Pay_Fragment;
import com.pdsu.graduation_design.views.Pre_Fragment;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {


    //订单数据
    public static Map<String,List<OrderDTO>> orderDTOListsMaps;
    public static  List<Order> ordersList;

    //购物车数据
    public static Map<Integer,List<PayCommodityDTO>> payCommodityDTOListsMaps ;
    public static List<Store> storesList;
    //----------
    public static User user_admin;
    public static String URL="http://10.0.2.2:8088";
    //底部导航RadioGroup
    private RadioButton pre_r_btn;
    private RadioButton pay_r_btn;
    private RadioButton main_r_btn;
    private RadioButton news_r_btn;
    private RadioButton my_r_btn;
    //标题TextView
    //ViewPageManager
    private ViewPager viewPager;
    //FragmentList用于适配器
    List<Fragment> fragmentList;
    //FragmentAdapter
    private  M_FragmentPagerAdapter m_fragmentPagerAdapter;
    //底部按钮点击时间监听
    private Bottom_Tap_OnClickListener bottom_tap_onClickListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //隐藏任务栏
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        bind();
        initUser();
        initOrderData();

    }

    public static void initOrderData(){

        RequestParams requestParams=new RequestParams(MainActivity.URL+"/order/getOrderDTOListByOrderUID");
        requestParams.addParameter("uid",MainActivity.user_admin.getUid());
        x.http().post(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Result res2 = null;
                try {
                    res2 = JsonUtil.JsonToObj(result, Result.class);
                    if (res2.getCode().equals("200")){
                        List<OrderDTO> orderDTOS = JsonUtil.json2list(res2.getData().toString(),OrderDTO.class);
                        List<Order> orderList = new ArrayList<>();

                        Map<String,List<OrderDTO>> orderMaps = new HashMap<>();
                        for (OrderDTO o: orderDTOS){
//                            OrderDTO orderDTO_temp = new OrderDTO();
//                            orderDTO_temp.setOrder_number(o.getOrder_number());
                            if (!orderMaps.containsKey(o.getOrder_number())){
                                Order order=new Order();
                                order.setCid(o.getCid());
                                order.setOrder_number(o.getOrder_number());
                                order.setCname(o.getCname());
                                order.setUid(o.getUid());
                                order.setCreate_time(o.getCreate_time());
                                order.setState(o.getState());
                                orderList.add(order);
                                List<OrderDTO> orderDTOS_t=new ArrayList<>();
                                orderDTOS_t.add(o);
                                orderMaps.put(o.getOrder_number(),orderDTOS_t);
                            }else {
                                List<OrderDTO> orderDTOS_t=orderMaps.get(o.getOrder_number());
                                orderDTOS_t.add(o);
                                orderMaps.put(o.getOrder_number(),orderDTOS_t);
                            }

                        }

                        orderDTOListsMaps=orderMaps;
                        ordersList=orderList;


                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }

//                if ()


            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {

            }
        });






    }
    private void init() {
        fragmentList=new ArrayList<>();

        //初始化底部导航按钮
        pre_r_btn=findViewById(R.id.pre_btn);
        pay_r_btn=findViewById(R.id.pay_btn);
        main_r_btn=findViewById(R.id.main_btn);
        news_r_btn=findViewById(R.id.news_btn);
        my_r_btn=findViewById(R.id.my_btn);

        //viewpage
        viewPager=findViewById(R.id.main_viewpage);

        //Fragment
        Pre_Fragment pre_fragment=new Pre_Fragment();
        Pay_Fragment pay_fragment = new Pay_Fragment();
        Main_Fragment main_fragment = new Main_Fragment();
        News_Fragment news_fragment = new News_Fragment();
        My_Fragment my_fragment = new My_Fragment();

        //添加至List
        fragmentList.add(pre_fragment);
        fragmentList.add(news_fragment);
        fragmentList.add(main_fragment);
        fragmentList.add(pay_fragment);
        fragmentList.add(my_fragment);
        //
        m_fragmentPagerAdapter =new M_FragmentPagerAdapter(getSupportFragmentManager(),fragmentList);

        viewPager.setAdapter(m_fragmentPagerAdapter);

        bottom_tap_onClickListener=new Bottom_Tap_OnClickListener(viewPager);




    }

    private void bind() {
        //事件绑定
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

                if (state==2){

                    int currentItemPosition = viewPager.getCurrentItem();

                    switch (currentItemPosition){
                        case 0:
                            pre_r_btn.setChecked(true);
                            break;
                        case 1:
                            news_r_btn.setChecked(true);
                            break;
                        case 2:
                            main_r_btn.setChecked(true);
                            break;
                        case 3:
                            pay_r_btn.setChecked(true);
                            break;
                        case 4:
                            my_r_btn.setChecked(true);
                            break;

                }

            }
        }


    });
        pre_r_btn.setOnClickListener(bottom_tap_onClickListener);
        pay_r_btn.setOnClickListener(bottom_tap_onClickListener);
        main_r_btn.setOnClickListener(bottom_tap_onClickListener);
        news_r_btn.setOnClickListener(bottom_tap_onClickListener);
        my_r_btn.setOnClickListener(bottom_tap_onClickListener);


    }

    public static void initUser(){


        RequestParams requestParams= new RequestParams(MainActivity.URL+"/payCommodity/findPayCommodityByUID");
        requestParams.addParameter("uid",MainActivity.user_admin.getUid());
        x.http().post(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Result r= null;
                try {
                    r = JsonUtil.JsonToObj(result, Result.class);
                    if (r.getCode().equals("200")){
                        List<PayCommodityDTO> payCommodities2 = JsonUtil.json2list(r.getData().toString(), PayCommodityDTO.class);
                        setPayCommodities(payCommodities2);
                        System.out.println("获得购物车信息");
                    }else {
                        System.out.println("访问出错");
                    }

                } catch (Exception e) {

                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
   public  static void setPayCommodities( List<PayCommodityDTO> payCommodities2){

        //这是购物车中全部的商品
        //给其转化成 Map<String,list<paycommodities>>
        Map<Integer,List<PayCommodityDTO>> map = new HashMap<>();
        List<Store> stores=new ArrayList<>();

        for (PayCommodityDTO p:payCommodities2){
            if (map.get(p.getUid())==null){
                stores.add(new Store(p.getUsername(),p.getUid()));
                List<PayCommodityDTO> listpa=new ArrayList<>();
                listpa.add(p);
                map.put(p.getUid(),listpa);
            }else {
                List<PayCommodityDTO> listpa=map.get(p.getUid());
                listpa.add(p);
                map.put(p.getUid(),listpa);
            }
        }
       MainActivity.payCommodityDTOListsMaps=map;
        MainActivity.storesList=stores;
    }

}